package org.homerepair

class ContractorUser {
	
	static hasMany = [bids:Bid]
	
	transient springSecurityService

	String username
	String password
	String companyName
	String firstName
	String lastName
	String email
	String website
	byte[] companyLogo
	String address1
	String address2
	String city
	String state
	int zipCode
	boolean enabled
	boolean accountExpired
	boolean accountLocked
	boolean passwordExpired

	static constraints = {
		username blank: false, unique: true
		password blank: false
		firstName blank: false
		lastName blank: false
		website url: true
		companyLogo maxSize: 10000
		email blank: false, email: true
		address1 blank: false
		city blank: false
		state inList: ['FL', 'NJ']
		zipCode max: 99999, min: 00000
		
	}

	static mapping = {
		password column: '`password`'
	}

	Set<ContractorRole> getAuthorities() {
		ContractorUserContractorRole.findAllByContractorUser(this).collect { it.contractorRole } as Set
	}

	def beforeInsert() {
		encodePassword()
	}

	def beforeUpdate() {
		if (isDirty('password')) {
			encodePassword()
		}
	}

	protected void encodePassword() {
		password = springSecurityService.encodePassword(password)
	}
}
